package types

import (
	"cuelang.org/go/cue/ast"
	"fmt"
	"strings"
)

type List struct {
	vv *ast.ListLit
}

func (t *List) Value(ref bool) string {
	tRef := t.vv.Elts[0].(*ast.Ellipsis).Type
	switch tRef.(type) {
	case *ast.SelectorExpr:
		vv := tRef.(*ast.SelectorExpr)
		mType := vv.X.(*ast.Ident).Name
		sType := vv.Sel.(*ast.Ident).Name
		if strings.HasPrefix(sType, "#") {
			format := "[]%s.%s"
			if ref {
				format = "[]*%s.%s"
			}
			return fmt.Sprintf(format, mType, strings.TrimPrefix(sType, "#"))
		}
		return fmt.Sprintf("%s.%s", mType, sType)
	default:
		sType := tRef.(*ast.Ident).Name
		if strings.HasPrefix(sType, "#") {
			format := "[]%s"
			if ref {
				format = "[]*%s"
			}
			return fmt.Sprintf(format, strings.TrimPrefix(sType, "#"))
		}
		return fmt.Sprintf("[]%s", sType)
	}

}

func (t *List) Default() string {
	return ""
}
